#!/usr/bin/env python
#coding:utf-8

################################
# file: httpHelp.py            #
# function:                    #
# author: xsseroot@gmail.com   #
# time: 2014-9-2 14:03         #
# update: 2014-9-4 16:34:11    #
################################

import time
import cookielib
import re
import sys
import socket
import os
import httplib

from urllib2 import urlopen

from lib.core.log import logger as log
from lib.models.request import Request

class HttpParse():
    """
    http 解析类
    """
    __NumDic={1:"first",2:"seconds",3:"third",4:"fourth",5:"fifth",6:"sixth",7:"seventh",8:"eighth",9:"Ninth"}
    delay = 1
    maxtry = 3
    timeout = 5
    header = {"Content-type":"application/x-www-form-urlencodeed",
              "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*",
              "Accept-Language":"zh-CN,zh;q=0.8",
              "UserAgent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 SE 2.X MetaSr 1.0"}
	
    def __init__(self, headers=None):
        if headers:
            self.header = headers
            
    #获取url中的html源码
    def getHtml(self, req):
        html = ""
        conn = httplib.HTTPConnection(req.domain, req.port, True, req.timeout)        
        for i in range(1,self.maxtry+1):#失败后尝试连接
            if i != 1:
                log.debug("Try the %s connection..." %  self.__NumDic[i])
            try:
                log.debug("uri:%s" % req.uri)
                conn.request(req.method, req.uri, "" , self.header);
                response = conn.getresponse()
                log.debug("Status code:%s, %s" % (str(response.status),req.url))
                if response.status == 200:                    
                    #print req.decode
                    if req.decode:
                        html = response.read().decode(req.decode)	
                    else:
                        html = response.read()
                    #log.debug("html:%s" % html)
                    break             
            except Exception as e:
                log.error( "%s, request host:%s, port:%s fail. %s" % (str(e),req.domain,req.port,req.url))
            finally:			   
                conn.close()
            if self.delay > 0 and self.maxtry != i:
                time.sleep(self.delay)
        return html
        
    #根据正则匹配url中的html或字符串，返回list列表  
    def getList(self, req, regex, type="text"):
        result = []
        if type == "url":
            html = self.getHtml(req)
        else:
            html = htmlorurl
        if html:
            listpage = regex.search(html)
            if listpage:
                result = listpage.group(1)
            else:
                result = None
        return result
        
    #根据正则匹配当前url中html，返回一个html元素innerText内容    
    def getInnerText(self, req, regex):
        result = None
        html = self.getHtml(req)
        #print html
        #log.debug(html)
        if html:
            result = regex.findall(html)
            if result:
                result = result[1]
        return result

if __name__ == "__main__":
    rootPath = "%s" % os.path.dirname(os.path.dirname(os.getcwd()))
    sys.path.append(rootPath)
    from lib.core.log import logger as log
    from lib.models.request import Request
    html = HttpParse()
    req = Request("http://www.22mm.cc/mm/jingyan/index.html", 5, "utf-8")
    log.info( html.getHtml(req))
    #print sys.getfilesystemencoding()
    #imgDiv = html.getInnerText(url,re.compile(r'<div class="c_inner">(.*?)</div>'))
    #print imgDiv
    
    
